
var DocsPollerPack = window.DocsPollerPack || {};

function makeHttpObject() {
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Could not create HTTP request object.");
}

DocsPollerPack.ModelInterface = function()
{
        //Nothing here, this is just an interface
}

DocsPollerPack.ModelInterface.prototype.getTable = function()
{
        alert("Unimplemented Method");
}
DocsPollerPack.ModelInterface.prototype.getCandidateTotal = function()
{
        alert("Unimplemented Method");
}
DocsPollerPack.ModelInterface.prototype.addsubscriptor = function(subscriptor)
{
        alert("Unimplemented Method");
}

DocsPollerPack.ModelInterface.prototype.getRegionData = function(keys) 
{
        alert("Unimplemented Method");
}

//----------------------------
DocsPollerPack.Model = function()
{
	this.candidateTotal = [];
	this.names =[];
	this.regionNameList = [];
    
}
DocsPollerPack.Model.prototype = clone(DocsPollerPack.ModelInterface.prototype);
DocsPollerPack.Model.prototype.constructor = DocsPollerPack.Model;
DocsPollerPack.Model.prototype.addsubscriptor = function(viewSubscriptor)
{
        this.subscriptor = viewSubscriptor;
}
DocsPollerPack.Model.prototype.getTable = function()
{
	var bundle = [];
	bundle.push(this.regionNameList[0]);
	bundle.push(this.names);
	bundle.push(this.candidateTotal);
	return bundle;
}
DocsPollerPack.Model.prototype.getCandidateTotal = function()
{
	var self = this;
	var request = makeHttpObject();
	request.open("GET", "http://127.0.0.1:9292/database/cdtotal", true);
	request.send(null);
	request.onreadystatechange = function() {
	 if (request.readyState == 4){
		self.names = (JSON.parse(request.responseText)).Result[0];
		self.candidateTotal = (JSON.parse(request.responseText)).Result[1];
		self.regionNameList[0] = "----"
		self.subscriptor.notify()
		
	 }
	};
	
	
}
DocsPollerPack.Model.prototype.getRegionData = function()
{
	var self = this;
	var request = makeHttpObject();
	request.open("GET", "http://127.0.0.1:9292/database/detailnw", true);
	request.send();
	request.onreadystatechange = function() {
	 if (request.readyState == 4){
		
		self.subscriptor.notify()
		
	 }
	};
	
}